<template>
  <div class="pageBox">
    <head-component title="商品详情" :borderStatus="false"></head-component>
    <div class="swipeBox">
      <van-swipe @change="onChange">
        <van-swipe-item v-if="details.video">
          <video class="videoObj" :src="details.video" controls></video>
        </van-swipe-item>
        <van-swipe-item v-for="(item, index) in swipeData" :key="index">
          <img class="banner" :src="item" alt="" />
        </van-swipe-item>
        <template #indicator>
          <div class="custom-indicator">
            {{ active + 1 }}/{{
              details.video ? swipeData.length + 1 : swipeData.length
            }}
          </div>
        </template>
      </van-swipe>
    </div>

    <div class="priceBox">
      <div class="price flex-row">
        <p>￥{{ nowPrice }}</p>
        <del>￥{{ oldPrice }}</del>
        <span v-if="details.genre == 2"
          >赠送{{ details.pcate ? details.pcate.sendScore : "" }}倍积分</span
        >
      </div>
      <div class="title">{{ details.title }}</div>
    </div>

    <div class="line"></div>

    <div class="detailsBox">
      <div class="detailsTitle">商品详情</div>
      <div class="con" ref="con" v-html="details.content"></div>
    </div>

    <img
      class="fanhuidingbu_btn"
      src="@/image/home/fanhuidingbu_btn.png"
      alt=""
      @click="onBackTop"
    />

    <div class="bottom flex-between">
      <div class="rightItem flex-row">
        <button class="buy" @click="onBuy">
          {{ details.genre == 0 ? "立即兑换" : "立即购买" }}
        </button>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { defineComponent, nextTick, ref } from "vue";
import headComponent from "../../components/headComponent.vue";
import router from "../../router";
import { post } from "../../untils/request";

export default defineComponent({
  components: {
    headComponent,
  },
  setup() {
    const con = ref(null);
    const id = ref(
      router.currentRoute.value.query.id
        ? router.currentRoute.value.query.id
        : ""
    );
    const inviteCode = ref(
      router.currentRoute.value.query.inviteCode
        ? router.currentRoute.value.query.inviteCode
        : ""
    );
    const active = ref(0);
    const swipeData = ref([]);

    const isChecked = ref(false); // 是否选中规格

    const details = ref({}); // 详情
    const hasoption = ref(0); // 是否启用规格1：启用 0：不启用

    const oldPrice = ref(0); // 原价
    const nowPrice = ref(0); // 现价

    // 轮播变换
    function onChange(index) {
      active.value = index;
    }

    // 回到顶部
    function onBackTop() {
      document.documentElement.scrollTop = 0;
    }

    // 点击页面底部立即购买
    function onBuy() {
      let url =
        process.env.VUE_APP_URL +
        "/h5/#/register?inviteCode=" +
        inviteCode.value;
      router.replace(url);
    }

    // 获取详情
    function getDetails() {
      post("/portal/Shop", {
        module: "Goods",
        interface: "2002",
        data: {
          id: id.value,
          lat: "0",
          lng: "0",
        },
      }).then((res) => {
        console.log("商品详情：", res);
        details.value = res.data;

        swipeData.value = res.data.thumb;
        hasoption.value = res.data.hasoption;
        oldPrice.value = res.data.productprice;
        nowPrice.value = res.data.marketprice;
        nextTick(() => {
          addControls();
        });
      });
    }
    getDetails();

    function addControls() {
      let videos = con.value.getElementsByTagName("video");
      for (let i = 0; i < videos.length; i++) {
        videos[i].controls = true;
      }
    }

    return {
      con,
      id,
      active,
      swipeData,
      details,
      isChecked,
      hasoption,
      oldPrice,
      nowPrice,
      inviteCode,
      onChange,
      onBackTop,
      onBuy,

      getDetails,
      addControls,
    };
  },
});
</script>

<style scoped lang="less">
.pageBox {
  width: 100%;
  min-height: 100vh;
  padding-top: 88px;
  padding-bottom: 110px;
  .swipeBox {
    width: 100%;
    height: 750px;
    :deep(.van-swipe) {
      width: 100%;
      height: 750px;
    }
    :deep(.van-swipe-item) {
      width: 100%;
      height: 750px;
    }
    .custom-indicator {
      position: absolute;
      right: 30px;
      bottom: 20px;
      padding: 2px 5px;
      font-size: 12px;
      width: 80px;
      height: 46px;
      background: rgba(23, 24, 26, 0.5);
      border-radius: 26px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 24px;
      font-weight: 400;
      color: #ffffff;
    }
    .videoObj {
      width: 100%;
      height: 100%;
    }
    .banner {
      width: 100%;
      height: 100%;
    }
  }
  .priceBox {
    height: 213px;
    background-color: #ffffff;
    padding: 40px 30px;
    .price {
      align-items: flex-end;
      p {
        font-size: 42px;
        font-weight: 500;
        color: #e00b12;
      }
      del {
        font-size: 28px;
        font-weight: 400;
        color: #cccccc;
        padding-bottom: 4px;
        padding-left: 22px;
      }
      span {
        font-size: 28px;
        font-weight: 500;
        color: #e00b12;
        padding-left: 22px;
        padding-bottom: 4px;
      }
    }
    .title {
      font-size: 36px;
      font-weight: 500;
      color: #333333;
      margin-top: 24px;
    }
  }
  .line {
    width: 100%;
    height: 20px;
    background-color: #f5f5f5;
  }
  .detailsTitle {
    height: 105px;
    font-size: 32px;
    font-weight: 500;
    color: #333333;
    padding: 30px 0 0 33px;
  }
  .con {
    padding: 0 30px;
    :deep(img) {
      max-width: 100%;
    }
  }
  .fanhuidingbu_btn {
    width: 60px;
    height: 60px;
    position: fixed;
    bottom: 130px;
    right: 30px;
    z-index: 1;
  }
  .bottom {
    width: 100%;
    height: 110px;
    background: #ffffff;
    box-shadow: 0px -1px 0px 0px #dadee6;
    padding: 0 30px 0 46px;
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 1;

    .rightItem {
      width: 100%;
      .buy {
        width: 100%;
        height: 80px;
        background: linear-gradient(270deg, #b30b11 0%, #f50811 100%);
        border-radius: 40px;
        font-size: 26px;
        font-weight: 400;
        color: #ffffff;
        border: 1px solid #e00b12;
      }
    }
  }
}
</style>